package controllers;

import files.MyFile;
import files.Save;
import gui.WelcomeUI;

import java.awt.Desktop;
import java.io.File;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Hashtable;
import java.util.Observable;
import java.util.Observer;

import Entity.Book;
import Entity.BookCopy;
import Entity.User;
import messagesPacks.*;

/**
 * This class is represent the controller of librarian
 * @author Tali Pisarev and Alon Shahaf
 * 	 
 */

public class LibrarianUIP implements Observer
{
	
	public LibrarianUIP()
	{
		System.out.println("LibrarianUIP Created.");
		WelcomeUIP.client.addObserver(this);
	}
	

	/**
	* @author Tali Pisarev
	* This method-librarianSearchUser get id of user and send request for search user  
	* @param Sring userID 
	* @return LibrarianSearchUserMessage (all the details of subscriber)
	* @throws Exception e when there is a mistake
	*/
	
	public  LibrarianSearchUserMessage librarianSearchUser (String userID )
	{
		LibrarianSearchUserMessage resultSearchUserMsg = new LibrarianSearchUserMessage(userID);
		try
		{
			System.out.println("Sending librarianSearchUserMessage & WAITING..");
			WelcomeUIP.client.setMaxRetries(2);
			WelcomeUIP.client.setWaitTime(6000);
			Object return_obj = null;
			return_obj = WelcomeUIP.client.sendToServerAndWait(resultSearchUserMsg, resultSearchUserMsg);
			 
			if (return_obj==null)
			{
				// Timeout OR error in transmit.
				System.out.println("UIP: (NULL) in librarianSearchUser() ");
				return resultSearchUserMsg;
			}
			else
			{
				resultSearchUserMsg = (LibrarianSearchUserMessage)return_obj;
				return resultSearchUserMsg;
			}
		 }
		 catch (Exception e) 
		 {
				System.out.println("UIP: EXCEPTION in SubscriberSearchForBook ");
				e.printStackTrace();
				return resultSearchUserMsg;
		 }
	}
	/**
	* @author Tali Pisarev
	* This method-librarianSearchBook get the parameters on which to search and returns the details of book   
	* @param String bookName
	* @param  String bookAuthor
	* @param  String bookSubject
	* @param  String bookSubjectID
	* @param  String freeText  
	* @return SubscriberSearchBookMassege (all the details of subscriber)
	* @throws Exception e when there is a mistake
	*/
	public SubscriberSearchBookMassege librarianSearchBook(String bookName , String bookAuthor, String bookSubject ,String bookSubjectID , String freeText )
	{
		

	   //if the librarian didn't fill any details about the searched book 
	  if(bookName.isEmpty() && bookAuthor.isEmpty() && bookSubject.equals("Please select subject...") && freeText.isEmpty() )
		  return null;
	  
	  SubscriberSearchBookMassege searchMsg  = new SubscriberSearchBookMassege();
	  SubscriberSearchBookMassege resultSearchMsg  = new SubscriberSearchBookMassege();
	  
	  searchMsg.getBook().setBookName(bookName);
	  searchMsg.getBook().setBookAuthor(bookAuthor);
		 if( !bookSubject.equals("Please select subject...") ) 
		   {
			 searchMsg.getBook().setBookSubject(bookSubject);
			 searchMsg.getBook().setBookSubjectID(bookSubjectID);
		   }
		 else
		 {
			 searchMsg.getBook().setBookSubject("");
			 searchMsg.getBook().setBookSubjectID(""); 
		 }
	  searchMsg.setFreeText(freeText);

	  try
		{
			System.out.println("Sending LibrarianSearchBookMassege & WAITING..");
			WelcomeUIP.client.setMaxRetries(2);
			WelcomeUIP.client.setWaitTime(6000);
			Object return_obj = null;
			return_obj = WelcomeUIP.client.sendToServerAndWait(searchMsg, searchMsg);
			 
			if (return_obj==null)
			{
				// Timeout OR error in transmit.
				System.out.println("UIP: (NULL) in scriberSearchForBook() ");
				resultSearchMsg .setQueryResult(0);
				return resultSearchMsg;
			}
			else
			{
				return ( SubscriberSearchBookMassege)return_obj; 
			}
		 }
		 catch (Exception e) 
		 {
				System.out.println("UIP: EXCEPTION in SubscriberSearchForBook ");
				e.printStackTrace();
				resultSearchMsg .setQueryResult(0);
				return resultSearchMsg;
		 }

	}
	
	
	/**
	* @author Tali Pisarev
	* This method-createUser get all the details of new user   
	* @param String id
	* @param  String pass
	* @param  String fname
	* @param  String lname
	* @param  String address
	* @param  String email
	* @param  String phone
	* @return 1 if user created successfully
	* 		 -1 if user already exists
	*        -99 if operation failed
	* @throws Exception e when there is a mistake
	*/
    //Create user
	public int createUser(String id,String pass, String fname, String lname,String address, String email, String phone)
	{
		System.out.println("LibrarianUIP: (createUser) ID=" + id + "FName=" + fname + "LName=" + lname);
		// Create a new message to add a new user
		UsersMSG newUserMsg = new UsersMSG(UsersMSG.op_type.ALL_createUser);
		newUserMsg.addUser(0, new User(id,pass,fname,lname,address,email,phone,3,0));
		
		WelcomeUIP.client.setMaxRetries(2);
		WelcomeUIP.client.setWaitTime(3000);
		Object return_obj = null;
		return_obj = WelcomeUIP.client.sendToServerAndWait(newUserMsg, new SimpleIntMessage(SimpleIntMessage.op_type.ALL_createUser,1));
		
		if (return_obj==null)
		{
			// Timeout OR error in transmit.
			System.out.println("LibrarianUIP: (createUser) result=NULL");
		}
		else
		{
			System.out.println("LibrarianUIP: (createUser) result = " + ((SimpleIntMessage)return_obj).getResult());
			if (((SimpleIntMessage)return_obj).getResult() == 1)
			{
				System.out.println("LibrarianUIP: (createUser) user created successfully!");	
				return 1;
			}
			else if(((SimpleIntMessage)return_obj).getResult() == -1)
			{
				System.out.println("LibrarianUIP: (createUser) user already exists!");
				return -1;
			}
		}
		return -99;
	}
	
	/**
	 * Borrows a copy from the library to a user.
	 * @author Alon Shahaf
	 * @Data 06.01.2013
	 * @param isbn	the book code
	 * @param copyID	the copy id to be borrowed
	 * @param user	the borrower details
	 * @return -99 - Operation failed.
	 * 		   -10 - Borrow Failed - this copy is reported lost!	
	 * 			-9 - Borrow Failed - user is not a subscriber!
	 * 			-8 - Borrow Failed - user not found.
	 * 			-7 - Borrow Failed - Input error.
	 * 			-6 - Borrow Failed - unable to delete user's order!
	 * 			-5 - Borrow Failed - there are earlier orders for this book!
	 * 			-4 - Borrow Failed - this copy is already borrowed!
	 * 			-3 - Borrow Failed - user is already borrowing this book!
	 * 			-2 - Borrow Failed - user is suspended!
	 * 			-1 - Borrow Failed - user is locked!
	 * 			 1 - Borrow OK.
	 */
	public int borrowCopy(String isbn,String copyID,User user)
	{
		// 1.	Check input
		if ((isbn == null) || (isbn.isEmpty()) || (copyID == null) || (copyID.isEmpty()) || (user == null) || (user.getID().isEmpty())) 
			return -7;
		
		// 2.	Create copy request info message
		BorrowMessage rqst_copy_msg = new BorrowMessage(user);
		rqst_copy_msg.setCopy(new BookCopy(copyID,"",isbn));		// put this copy to the message pack.	
		
		// 3.	Send the message to the server
		WelcomeUIP.client.setMaxRetries(2);
		WelcomeUIP.client.setWaitTime(3000);
		Object return_obj = null;
		return_obj = WelcomeUIP.client.sendToServerAndWait(rqst_copy_msg, new SimpleIntMessage(SimpleIntMessage.op_type.LIBRARIAN_borrowCopy,1));
		
		if (return_obj==null)
		{
			// Timeout OR error in transmit.
			System.out.println("LibrarianUIP: (borrowCopy) result=NULL");
			return -99;
		}
		SimpleIntMessage bres = ((SimpleIntMessage)return_obj);
		System.out.println("LibrarianUIP: (borrowCopy) result for user ID = " + user.getID() + " and book copy ID = " + copyID);
		
		return bres.getResult();
	}
	
	/**
	 * Creates a manual extension for a borrow. fired-up by the librarian.
	 * @author Alon Shahaf
	 * @Data 12.01.2013
	 * @param borrow_num the borrow number
	 * @param old_end_date_string the current return date of the borrow
	 * @param new_end_date_string the new wanted return date of the borrow.
	 * @return -99 - Operation failed.
	 * 	 		-4 - Extension Failed - input end date is before actual return date
	 * 			-3 - Extension Failed - Cannot extend "wanted" books.
	 * 			-2 - Extension Failed - Cannot extend over OBLSettings.MANUAL_EXT_MAX_DAYS days.
	 * 			-1 - Extension Failed - Input error
	 * 			 1 - Manual Extension OK.
	 */
	public int manualExtension(int borrow_num, String old_end_date_string,String new_end_date_string)
	{
		System.out.println("LibrarianUIP: (manualExtension) BN=" + borrow_num + "New end Date= " + new_end_date_string);
		// Create a new message
		ManualExtMessage mExtMsg = new ManualExtMessage();
		mExtMsg.setBorrow_num(borrow_num);
		// Convert entered new end date to date format
		try 
		{
			Date new_end_date = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").parse(new_end_date_string); //TODO changed - to /
			Date old_end_date = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").parse(old_end_date_string);
			
			mExtMsg.setNew_end_date(new_end_date);
			if (old_end_date.after(new_end_date))
				return -4;
			// Check for maximum Ext. borrow day
			Calendar cal = Calendar.getInstance();
			cal.setTime(old_end_date);
			cal.add(Calendar.DATE,OBLSettings.MANUAL_EXT_MAX_DAYS);
			Date max_possible_end_date = cal.getTime();
			if (new_end_date.after(max_possible_end_date))
				return -2;
		}
		catch (ParseException e) 
		{
			System.out.println("LibrarianUIP: (manualExtension) Error converting string to date!");
			e.printStackTrace();
			return -1;
		}
		catch (NullPointerException e)
		{
			System.out.println("LibrarianUIP: (manualExtension) Error comparing dates!");
			return -1;
		}
		System.out.println("LibrarianUIP: (manualExtension) SENDING MSG & WAITING");
		
		WelcomeUIP.client.setMaxRetries(2);
		WelcomeUIP.client.setWaitTime(1500);
		Object return_obj = null;
		return_obj = WelcomeUIP.client.sendToServerAndWait(mExtMsg, new SimpleIntMessage(SimpleIntMessage.op_type.LIBRARIAN_manualExt,1));
		
		if (return_obj==null)
		{
			// Timeout OR error in transmit.
			System.out.println("LibrarianUIP: (manualExtension) result=NULL");
		}
		else
		{
			System.out.println("LibrarianUIP: (manualExtension) result = " + ((SimpleIntMessage)return_obj).getResult());
			if (((SimpleIntMessage)return_obj).getResult() == 1)
			{
				System.out.println("LibrarianUIP: (manualExtension) Manual Extension was successfull!");	
				return 1;
			}
		}
		return -99;	
	}
	
	
	/**
	* @author Tali Pisarev
	* This method-editUser get all the details that need to edit  
	* @param String id
	* @param  String fname
	* @param  String lname
	* @param  String address
	* @param  String email
	* @param  String phone
	* @return 1 if edit user successfully
	*        -99 if operation failed
	* @throws Exception e when there is a mistake
	*/
	
	//edit user
	public int editUser(String id,String fname, String lname, String address, String email, String phone )
	{
		System.out.println("LibrarianUIP: (editUser) ID=" + id + "FName= " + fname + "LName= " + lname);
		// Create a new message to add a new user
		UsersMSG editUserMsg = new UsersMSG(UsersMSG.op_type.EditUser);
		editUserMsg.addUser(0, new User(id,fname,lname,address,email,phone));
		
		WelcomeUIP.client.setMaxRetries(2);
		WelcomeUIP.client.setWaitTime(3000);
		Object return_obj = null;
		return_obj = WelcomeUIP.client.sendToServerAndWait(editUserMsg, new SimpleIntMessage(SimpleIntMessage.op_type.EditUser,1));
		
		if (return_obj==null)
		{
			// Timeout OR error in transmit.
			System.out.println("LibrarianUIP: (editUser) result=NULL");
		}
		else
		{
			System.out.println("LibrarianUIP: (editUser) result = " + ((SimpleIntMessage)return_obj).getResult());
			if (((SimpleIntMessage)return_obj).getResult() == 1)
			{
				System.out.println("LibrarianUIP: (editUser) user edit successfully!");	
				return 1;
			}

		}
		return -99;
	}
	/**
	* @author Tali Pisarev
	* This method-librarianAddBook get all the details of new book
	* @param String bookIsbn
	* @param  String bookName
	* @param  String bookAuthor
	* @param  String editionNum
	* @param  String publishYear
	* @param  String description
	* @param  String ShelfLocation
	* @param  String wanted
	* @param selsectBookSubjects --> hashtable that saves all the subject of this book
	* @param TOCfile 
	* @return 1 if edit user successfully
	*        -1 if operation failed
	* @throws Exception e when there is a mistake in  message
	*/
	//Add Book
	public  int librarianAddBook (String bookIsbn,String bookName,String bookAuthor,String editionNum,String publishYear,
			String description,String ShelfLocation, String wanted,Hashtable<Integer,String > selsectBookSubjects , byte [] TOCfile )
	{
		LibrarianAddBookMessage resultAddBookMsg = new LibrarianAddBookMessage();
		
		Hashtable<Integer,String > bookSubjects = new Hashtable<Integer,String>();
		bookSubjects.putAll(selsectBookSubjects);
		resultAddBookMsg.setBookSubjects(bookSubjects);
		if(TOCfile != null)
			 resultAddBookMsg.getBook().setTOCofBook(TOCfile);
		
		Book book = new  Book(bookIsbn, bookName, bookAuthor, editionNum, publishYear, description, ShelfLocation,  wanted );
		resultAddBookMsg.setBook(book);
		try
		{
			System.out.println("Sending librarianAddBookMessage & WAITING..");
			WelcomeUIP.client.setMaxRetries(2);
			WelcomeUIP.client.setWaitTime(6000);
			Object return_obj = null;
			return_obj = WelcomeUIP.client.sendToServerAndWait(resultAddBookMsg, resultAddBookMsg);
			 
			if (return_obj==null)
			{
				// Timeout OR error in transmit.
				System.out.println("UIP: (NULL) in librarianAddBook() ");
				return -1;
			}
			else
			{
				resultAddBookMsg = (LibrarianAddBookMessage)return_obj;
				return resultAddBookMsg.getQueryLibAddBookResult();
			}
		 }
		 catch (Exception e) 
		 {
				System.out.println("UIP: EXCEPTION in librarianAddBook ");
				e.printStackTrace();
				return -99;
		 }
	}
   
	/**
	* @author Tali Pisarev
	* This method-librarianEditBook get all the details of new book
	* @param String bookIsbn
	* @param  String bookName
	* @param  String bookAuthor
	* @param  String editionNum
	* @param  String publishYear
	* @param  String description
	* @param  String ShelfLocation
	* @param  String wanted
	* @param selsectBookSubjects --> hashtable that saves all the subject of this book
	* @param TOCfile 
	* @return 1 if edit user successfully
	*        -1 if book already exist
	*        -99 if operation failed
	* @throws Exception e when there is a mistake in  message
	*/
	//Edit Book
	public  int librarianEditBook (String bookIsbn,String bookName,String bookAuthor,String editionNum,String publishYear,String description,
			String ShelfLocation, String wanted,Hashtable<Integer,String > selsectBookSubjects , byte [] TOCfile )
	{
		LibrarianEditBookMessage resultEditBookMsg = new LibrarianEditBookMessage();
		Hashtable<Integer,String > bookSubjects = new Hashtable<Integer,String>();
		bookSubjects.putAll(selsectBookSubjects);
		resultEditBookMsg.getBook().setISBN(bookIsbn);
		resultEditBookMsg.getBook().setBookName(bookName);
		resultEditBookMsg.getBook().setBookAuthor(bookAuthor);
		resultEditBookMsg.getBook().setEdition(editionNum);
		resultEditBookMsg.getBook().setPublishYear(publishYear);
		resultEditBookMsg.getBook().setDescription(description);
		resultEditBookMsg.getBook().setShelfLocation(ShelfLocation);
		resultEditBookMsg.getBook().setWanted(wanted);
		resultEditBookMsg.setBookSubjects(bookSubjects);
		if(TOCfile != null)
		 resultEditBookMsg.getBook().setTOCofBook(TOCfile);
		
		
		try
		{
			System.out.println("Sending librarianAddBookMessage & WAITING..");
			WelcomeUIP.client.setMaxRetries(2);
			WelcomeUIP.client.setWaitTime(6000);
			Object return_obj = null;
			return_obj = WelcomeUIP.client.sendToServerAndWait(resultEditBookMsg, resultEditBookMsg);
			 
			if (return_obj==null)
			{
				// Timeout OR error in transmit.
				System.out.println("UIP: (NULL) in librarianEditBook() ");
				return -1; 
			}
			else
			{
				resultEditBookMsg = (LibrarianEditBookMessage)return_obj;
				System.out.println(resultEditBookMsg);
				return resultEditBookMsg.getQueryLibEditBookResult();
			}
		 }
		 catch (Exception e) 
		 {
				System.out.println("UIP: EXCEPTION in librarianAddBook ");
				e.printStackTrace();
				return -99;
		 }
	}
	
	/**
	* @author Tali Pisarev
	* This method-librarianSearchCopy get copyID
	* @param String copyID
	* @return LibrarianSearchCopyMessage (all the details of copy) if edit user successfully
	*        -1 if book already exist
	*        -99 if operation failed
	* @throws Exception e when there is a mistake in  message
	*/
	public  LibrarianSearchCopyMessage librarianSearchCopy (String copyID )
	{
		LibrarianSearchCopyMessage resultSearchCopyMsg = new LibrarianSearchCopyMessage(copyID);
		try
		{
			System.out.println("Sending librarianSearchCopyMessage & WAITING..");
			WelcomeUIP.client.setMaxRetries(2);
			WelcomeUIP.client.setWaitTime(6000);
			Object return_obj = null;
			return_obj = WelcomeUIP.client.sendToServerAndWait(resultSearchCopyMsg, resultSearchCopyMsg);
			 
			if (return_obj==null)
			{
				// Timeout OR error in transmit.
				System.out.println("UIP: (NULL) in librarianSearchCopy() ");
				return resultSearchCopyMsg;
			}
			else
			{
				resultSearchCopyMsg = (LibrarianSearchCopyMessage)return_obj;
				return resultSearchCopyMsg;
			}
		 }
		 catch (Exception e) 
		 {
				System.out.println("UIP: EXCEPTION in SubscriberSearchForCopy ");
				e.printStackTrace();
				return resultSearchCopyMsg;
		 }
	
	}
	
	/**
	* @author Tali Pisarev
	* This method-AddCopy get all the details of new copy
	* @param String copyID
	* @param  String ISBN
	* @param  String purchaseDate
	* @param  String borrow
	* @return 1 if edit user successfully
	*        -1 if copy already exist
	*        -99 if operation failed
	* @throws Exception e when there is a mistake in  message
	*/
    //Add Copy
	public int AddCopy(String copyID,String ISBN, String purchaseDate, String borrow)
	{
		LibrarianAddCopyMessage resultAddCopyMsg = new LibrarianAddCopyMessage();
		
		BookCopy bookCopy = new  BookCopy (copyID, ISBN, purchaseDate, borrow);
		resultAddCopyMsg.setCopy(bookCopy);
		try
		{
			System.out.println("Sending librarianAddBookMessage & WAITING..");
			WelcomeUIP.client.setMaxRetries(2);
			WelcomeUIP.client.setWaitTime(6000);
			Object return_obj = null;
			return_obj = WelcomeUIP.client.sendToServerAndWait(resultAddCopyMsg, resultAddCopyMsg);
			 
			if (return_obj==null)
			{
				// Timeout OR error in transmit.
				System.out.println("UIP: (NULL) in librarianAddCopy() ");
				return -1;
			}
			else
			{
				resultAddCopyMsg = (LibrarianAddCopyMessage)return_obj;
				return resultAddCopyMsg.getQueryLibAddCopyResult();
			}
		 }
		 catch (Exception e) 
		 {
				System.out.println("UIP: EXCEPTION in librarianAddCopy ");
				e.printStackTrace();
				return -99;
		 }
	}
	/**
	* @author Tali Pisarev
	* This method-editCopy get all the details that need to change
	* @param  String purchaseDate
	* @return 1 if edit user successfully
	*        -99 if operation failed
	* @throws Exception e when there is a mistake in  message
	*/
	//Edit Copy
	public  int editCopy (String copyID,String purchaseDate)
	{
		LibrarianEditCopyMessage resultEditCopyMsg = new LibrarianEditCopyMessage();
		resultEditCopyMsg.getCopy().setCopyID(copyID);
		resultEditCopyMsg.getCopy().setPurchaseDate(purchaseDate);
		try
		{
			System.out.println("Sending librarianAddCopyMessage & WAITING..");
			WelcomeUIP.client.setMaxRetries(2);
			WelcomeUIP.client.setWaitTime(6000);
			Object return_obj = null;
			return_obj = WelcomeUIP.client.sendToServerAndWait(resultEditCopyMsg, resultEditCopyMsg);
			 
			if (return_obj==null)
			{
				// Timeout OR error in transmit.
				System.out.println("UIP: (NULL) in librarianEditCopy() ");
				return -1; 
			}
			else
			{
				resultEditCopyMsg = (LibrarianEditCopyMessage)return_obj;
				return resultEditCopyMsg.getQueryLibEditCopyResult();
			}
		 }
		 catch (Exception e) 
		 {
				System.out.println("UIP: EXCEPTION in librarianEditCopy ");
				e.printStackTrace();
				return -99;
		 }
	}
	
	/**
	* @author Tali Pisarev
	* This method-LibrarianSubjectMessage get ISBN and returns the all subject of this book
	* @param  String ISBN
	* @return LibrarianSubjectMessage (all subjects of this book) if edit user successfully
	*        -99 if operation failed
	* @throws Exception e when there is a mistake in  message
	*/
	public LibrarianSubjectMessage bookSubject (String ISBN)
	{
		LibrarianSubjectMessage BookSubject_msg = new LibrarianSubjectMessage();
		BookSubject_msg.setISBN(ISBN);
	try
	{
		System.out.println("Sending SearchByBookSubjectMSG & WAITING..");
		WelcomeUIP.client.setMaxRetries(2);
		WelcomeUIP.client.setWaitTime(6000);
		Object return_obj = null;
		return_obj = WelcomeUIP.client.sendToServerAndWait(BookSubject_msg, BookSubject_msg);
		 
		if (return_obj==null)
		{
			// Timeout OR error in transmit.
			System.out.println("UIP: (NULL) in getSubjectsOfBooks() ");
			return null;
		}
		else
		{
			return (LibrarianSubjectMessage)return_obj;
		}
	 }
	 catch (Exception e) 
	 {
			System.out.println("UIP: EXCEPTION in getSubjectsOfBooks() ");
			e.printStackTrace();
			return null;
	 }
	
   }
	
	/**
	* @author Tali Pisarev
	* This method-librarianRemoveCopy get copyID and remove this copy from DB
	* @param  String copyID
	* @return 1 if remove copy successfully
	*        -99 if operation failed
	* @throws Exception e when there is a mistake in message
	*/
	public int librarianRemoveCopy (String copyID)
	{
		LibrarianRemoveCopyMessage removeCopy_msg = new LibrarianRemoveCopyMessage();
		removeCopy_msg.setCopyID(copyID);
	try
	{
		System.out.println("Sending LibrarianRemoveCopyMSG & WAITING..");
		WelcomeUIP.client.setMaxRetries(2);
		WelcomeUIP.client.setWaitTime(6000);
		Object return_obj = null;
		return_obj = WelcomeUIP.client.sendToServerAndWait(removeCopy_msg, removeCopy_msg);
		 
		if (return_obj==null)
		{
			// Timeout OR error in transmit.
			System.out.println("UIP: (NULL) in LibrarianRemoveCopy() ");
			return -2;
		}
		else
		{
			removeCopy_msg = (LibrarianRemoveCopyMessage)return_obj;
			return removeCopy_msg.getQueryLibRemoveCopyResult();
			
		}
	 }
	 catch (Exception e) 
	 {
			System.out.println("UIP: EXCEPTION in LibrarianRemoveCopy() ");
			e.printStackTrace();
			return -2;
	 }
	
   }
	

	/**
	* @author Tali Pisarev
	* This method-librarianRemoveBook get ISBN and remove this book and all copies of this book from DB
	* @param  String ISBN
	* @return 1 if remove book successfully
	*        -99 if operation failed
	* @throws Exception e when there is a mistake in message
	*/	
	public int librarianRemoveBook (String ISBN)
	{
		LibrarianRemoveBookMessage removeBook_msg = new LibrarianRemoveBookMessage();
		removeBook_msg.setCopyID(ISBN);
	try
	{
		System.out.println("Sending LibrarianRemoveBookMSG & WAITING..");
		WelcomeUIP.client.setMaxRetries(2);
		WelcomeUIP.client.setWaitTime(6000);
		Object return_obj = null;
		return_obj = WelcomeUIP.client.sendToServerAndWait(removeBook_msg, removeBook_msg);
		 
		if (return_obj==null)
		{
			// Timeout OR error in transmit.
			System.out.println("UIP: (NULL) in LibrarianRemoveBook() ");
			return -2;
		}
		else
		{
			removeBook_msg = (LibrarianRemoveBookMessage)return_obj;
			return removeBook_msg.getQueryLibRemoveBookResult();
			
		}
	 }
	 catch (Exception e) 
	 {
			System.out.println("UIP: EXCEPTION in Librarian Remove Book() ");
			e.printStackTrace();
			return -2;
	 }
	
   }
	
	
	/**
	* @author Tali Pisarev
	* This method-librarianReturnBook get copyID and BN(Borrow Number) and update that book was returned in DB.
	* @param  String copyID
	* @param  String bn
	* @return 1 if remove book successfully
	*        -99 if operation failed
	* @throws Exception e when there is a mistake in message
	*/		
	public int librarianReturnBook (String copyID, int bn)
	{
		LibrarianReturnBookMessage returnBook_msg = new LibrarianReturnBookMessage();
		returnBook_msg.setCopyID(copyID);
		returnBook_msg.setBN(bn);
	try
	{
		System.out.println("Sending LibrarianReturnBookMSG & WAITING..");
		WelcomeUIP.client.setMaxRetries(2);
		WelcomeUIP.client.setWaitTime(6000);
		Object return_obj = null;
		return_obj = WelcomeUIP.client.sendToServerAndWait(returnBook_msg, returnBook_msg);
		 
		if (return_obj==null)
		{
			// Timeout OR error in transmit.
			System.out.println("UIP: (NULL) in LibrarianReturnBook() ");
			return -2;
		}
		else
		{
			returnBook_msg = (LibrarianReturnBookMessage)return_obj;
			return returnBook_msg.getQueryLibReturnBookResult();
			
		}
	 }
	 catch (Exception e) 
	 {
			System.out.println("UIP: EXCEPTION in Librarian Remove Book() ");
			e.printStackTrace();
			return -2;
	 }
	
   }
	
	/**
	 * Uploads a new PDF (TOC) file to the server.
	* @author Alon Shahaf
	* @param  file	the file to be uploaded
	* @param  isbn	the book code that will be linked to the file.
	* @return 1 for success, -1 otherwise.
	*/	
	public int uploadPDF (MyFile file, String isbn)
	{
		// Prepare the message to the server
		TOCMessage pdfFile = new TOCMessage(isbn);
		System.out.print(file.getName());
		pdfFile.setMyfile(file);
		try
		{
			System.out.println("Sending PDF & WAITING..");
			WelcomeUIP.client.setMaxRetries(2);
			WelcomeUIP.client.setWaitTime(6000);
			Object return_obj = null;
			return_obj = WelcomeUIP.client.sendToServerAndWait(pdfFile, new SimpleIntMessage(SimpleIntMessage.op_type.LIBRARIAN_tocUpload,1));
			 
			if (return_obj==null)
			{
				// Timeout OR error in transmit.
				System.out.println("UIP: (NULL) in uploadPDF() ");
				return -1;
			}
			else
			{
				SimpleIntMessage toc = (SimpleIntMessage)return_obj;
				if (toc.getResult() == 1)
				{
					System.out.println("UIP: (uploadPDF) TOC Uploaded to server successfully!");
					return 1;
				}
				return -1;
			}
		 }
		 catch (Exception e) 
		 {
			System.out.println("UIP: EXCEPTION in Librarian Remove Book() ");
			e.printStackTrace();
			return -1;
		 }
	}

	/**
	 * Deletes a PDF (TOC) file from the server.
	* @author Alon Shahaf
	* @param  isbn	the book code that it's file is meant to be deleted.
	* @return 1 for success, -99 otherwise.
	*/	
	public int deleteTOC(String isbn)
	{
		// Prepare delete message
		DeleteTOCMessage del_toc_msg = new DeleteTOCMessage();
		del_toc_msg.setIsbn(isbn);
		
		try
		{
			System.out.println("Sending request DELETE TOC & WAITING..");
			WelcomeUIP.client.setMaxRetries(2);
			WelcomeUIP.client.setWaitTime(6000);
			Object return_obj = null;
			return_obj = WelcomeUIP.client.sendToServerAndWait(del_toc_msg, new SimpleIntMessage(SimpleIntMessage.op_type.LIBRARIAN_tocDelete,1));
			 
			if (return_obj==null)
			{
				// Timeout OR error in transmit.
				System.out.println("UIP: (NULL) in deleteTOC() ");
				return -99;
			}
			else
			{
				SimpleIntMessage toc = (SimpleIntMessage)return_obj;
				if (toc.getResult() == 1)
				{
					System.out.println("UIP: (deleteTOC) TOC deleted successfully!");
					return 1;
				}
			}
		}
		catch (Exception e) 
		{
			System.out.println("UIP: EXCEPTION in deleteTOC ");
			e.printStackTrace();
			return -99;
		}
		return -99;
	}
	//---------------------------------------------------------------------------------------------
	
	@Override
	public void update(Observable arg0, Object arg1)
	{
		// TODO Auto-generated method stub
		
	}
	
	
}
